package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.adapter.FragmentTabAdapter;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.entity.EntityCommunity;
import com.yuwj.chinaa.entity.EntityGeneralAuth;
import com.yuwj.chinaa.fragment.FragmentAssets;
import com.yuwj.chinaa.fragment.FragmentMain;
import com.yuwj.chinaa.fragment.FragmentMe;
import com.yuwj.chinaa.fragment.FragmentTrade;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ActivityCollectUtil;
import com.yuwj.chinaa.util.SharedpreferenceUtil;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.vp_container)
    ViewPager viewPager;
    @BindView(R.id.tl_foot)
    TabLayout tabLayout;
    FragmentMain fragmentMain;
    FragmentTrade fragmentTrade;
    FragmentAssets fragmentAssets;
    FragmentMe fragmentMe;
    List<Fragment> fragmentList;
    FragmentTabAdapter tabAdapter;
    List<String> titleList;
    List<Integer> titleIconList;
    IPresenter presenter;


    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_main;
    }

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        return intent;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
        titleList = new ArrayList<>();
        titleList.add("首页");
        titleList.add("交易");
        titleList.add("资产");
        titleList.add("我的");

        titleIconList = new ArrayList<>();
        titleIconList.add(R.drawable.selector_tap_icon_main);
        titleIconList.add(R.drawable.selector_tap_icon_trade);
        titleIconList.add(R.drawable.selector_tap_icon_assets);
        titleIconList.add(R.drawable.selector_tap_icon_me);

        fragmentMain = FragmentMain.createInstance();
        fragmentTrade = FragmentTrade.createInstance();
        fragmentAssets = FragmentAssets.createInstance();
        fragmentMe = FragmentMe.createInstance();
        fragmentList = new ArrayList<>();
        fragmentList.add(fragmentMain);
        fragmentList.add(fragmentTrade);
        fragmentList.add(fragmentAssets);
        fragmentList.add(fragmentMe);

        tabAdapter = new FragmentTabAdapter(getSupportFragmentManager(), fragmentList);


    }

    @Override
    public void initView() {
        viewPager.setAdapter(tabAdapter);
        viewPager.setOffscreenPageLimit(4);//设置缓冲页面为4个,避免频繁销毁界面
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < fragmentList.size(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(R.layout.content_tab);

            TextView tvName = (TextView) tab.getCustomView().findViewById(R.id.tv_name);
            tvName.setText(titleList.get(i));
            ImageView ivIcon = (ImageView) tab.getCustomView().findViewById(R.id.iv_icon);
            ivIcon.setImageResource(titleIconList.get(i));

        }
        tabLayout.getTabAt(0).select();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 2 && fragmentAssets != null && fragmentAssets.isLoadSuccess) {
                    fragmentAssets.refreshUserBurseNumberList();
                }
                if (tab.getPosition() == 1 && fragmentTrade != null && fragmentTrade.isLoadSuccess()) {
                    fragmentTrade.updateUserAvailableGTE();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    @Override
    public void initOperator() {

    }

    private boolean isExit = false;// 标识是否退出系统

    @Override
    public void onBackPressed() {
        // 双击退出系统
        if (!isExit) {
            isExit = true;
            ToastUtil.getInstance().shortLength(this, "再按一次退出程序！");
            new Timer().schedule(new TimerTask() {

                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            // 退出程序,清除用户信息,清除header里面的token
            SharedpreferenceUtil.getInstance().clearUserInfo(context);
            OkGo.getInstance().getCommonHeaders().remove("token");
            ActivityCollectUtil.getInstance().finishAll();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    }


    public IPresenter getPresenter() {
        return presenter;
    }

    public void setPresenter(IPresenter presenter) {
        this.presenter = presenter;
    }



    public ViewPager getViewPager() {
        return viewPager;
    }

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
